package com.echoesnet.echoesshortvideo.Fragments;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.Activities.BindPhoneAct;
import com.echoesnet.echoesshortvideo.Activities.ReportFoulsUserAct;
import com.echoesnet.echoesshortvideo.Beans.ShareToFaceBean;
import com.echoesnet.echoesshortvideo.Beans.Video;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.NetUtil;
import com.echoesnet.echoesshortvideo.Utils.NetUtilListener;
import com.echoesnet.echoesshortvideo.Utils.SharePreUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.Utils.VideoUtils;
import com.echoesnet.echoesshortvideo.ViewModels.VideoDetailViewModel;
import com.echoesnet.echoesshortvideo.views.CommentListDialog;
import com.echoesnet.echoesshortvideo.views.InputTextMsgDialog;
import com.echoesnet.echoesshortvideo.views.MySwipeRefreshLayout;
import com.echoesnet.echoesshortvideo.views.MyVerticalViewPager;
import com.echoesnet.echoesshortvideo.views.adapters.SharesAdapter;
import com.echoesnet.echoesshortvideo.views.adapters.VerticalViewPagerAdapter2;
import com.echoesnet.echoesshortvideo.views.dialog.CustomAlertDialog;
import com.echoesnet.echoesshortvideo.views.video.EmptyControlVideo;
import com.flipboard.bottomsheet.BottomSheetLayout;
import com.flipboard.bottomsheet.OnSheetDismissedListener;
import com.like.LikeButton;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * The type Vertical video fragment.
 *
 * @author Administrator
 * @Date 2018 /4/23
 * @Version 1.0
 */
public class VerticalVideoFragment extends BaseFragment
{
    private final String TAG = VerticalVideoFragment.class.getSimpleName();

    private final int TRIGGER_DATEL = 3;

//    @BindView(R.id.verticalViewPager)
//    VerticalViewPager verticalViewPager;

    @BindView(R.id.verticalViewPager)
    MyVerticalViewPager verticalViewPager;
    @BindView(R.id.swipe_layout)
    MySwipeRefreshLayout mySwipeRefreshLayout;

    private TextView tvLoveNum;
    private TextView tvCommentsNum;
    private View bottomSheet;
    private TextView tvShareNum;
    private TextView tvFocus;
    private Unbinder unbinder;
    private Activity mActivity;
    private VideoDetailViewModel model;
    private int videoIndex;
    private String videoId = "";
    private String openSource = "";
    private String toUserId = "";
    private String getDataDirectory = "0";
    private String shareVideoId = "";

    private boolean isLiked;

    private VerticalViewPagerAdapter2 fragmentPagerAdapter;
    private List<Video> videoList = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> sharesName = new ArrayList<>();
    private InputTextMsgDialog inputMsgDialog;
    private SharesAdapter sharesAdapter;

    private int currentIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.vertical_video_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        afterViews();

    }

    private void afterViews()
    {

        mActivity = getActivity();
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            videoId = bundle.getString("videoId");
            openSource = bundle.getString("openSource");
            if ("1".equals(openSource))
                toUserId = bundle.getString("toUserId");
        }

        verticalViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {
            }

            @Override
            public void onPageSelected(int position)
            {
                Logger.t(TAG).d("------->onPageSelected():" + currentIndex + " " + position + " " + videoList.size());
                if (position != 0)
                    mySwipeRefreshLayout.setNeedSwipe(false);
//                Intent intent = new Intent(CommonConstant.EAM_BRD_ACTION_REQUEST_USERINFO);

//                intent.putExtra("otherUserId", uid);
//                mActivity.sendBroadcast(intent);
                String uid = videoList.get(position).getUserId();
                actionListener.loadUserInfo(uid);

                Map<String, Object> map = fragmentPagerAdapter.getVideoMap().get(position);
                if (map != null)
                {
                    EmptyControlVideo emptyControlVideo = (EmptyControlVideo) map.get("video");
                    ImageView imageView = (ImageView) fragmentPagerAdapter.getVideoMap().get(position).get("image");
                    if (emptyControlVideo != null)
                    {
                        emptyControlVideo.startPlayLogic();
                        imageView.setVisibility(View.GONE);
//                    fragmentPagerAdapter.hideBtnPlayImage();
                        new Handler().postDelayed(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                IMediaPlayer iMediaPlayers = emptyControlVideo.getGSYVideoManager().getMediaPlayer();
                                if (iMediaPlayers != null)
                                {
                                    if (!iMediaPlayers.isPlaying())
                                    {
                                        emptyControlVideo.startPlayLogic();
                                        imageView.setVisibility(View.GONE);
                                    }
                                }
                            }
                        }, 1000);
                    }
                }

//                onVideoResume();
                Logger.t(TAG).d("------->11111111111111111");
                if (position == 0)//下拉 取上十条数据
                {
                    String videoId = videoList.get(0).getVideoId();
                    getDataDirectory = "1";
                    model.startGetMoreVideoDetail(videoId, openSource, toUserId, getDataDirectory);
                }
                else if (position == videoList.size() - 1)//下拉 取上十条数据
                {
                    String videoId = videoList.get(videoList.size() - 1).getVideoId();
                    getDataDirectory = "2";
                    model.startGetMoreVideoDetail(videoId, openSource, toUserId, getDataDirectory);
                }

//                viewModel.requestMore();

                currentIndex = position;
                Logger.t(TAG).d("------->上拉  请求下10数据");
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });

        model = ViewModelProviders.of(this).get(VideoDetailViewModel.class);
        model.getVideoDetail().observe(this, videoDetailEntity ->
        {
            if (videoDetailEntity != null)
            {
                videoIndex = videoDetailEntity.getIndex();
                videoList.addAll(videoDetailEntity.getVideos());

                if (videoList.size() == 0)
                {
                    ToastUtils.showShort("该视频已删除");
                    mActivity.finish();
                }
//                fragmentPagerAdapter = new VerticalViewPagerAdapter(mActivity, videoIndex, getFragmentManager(), videoInfoList, verticalViewPager, openSource);
                if (videoIndex == 0)//首次进入时  第一个 开启下拉刷新
                {
                    mySwipeRefreshLayout.setNeedSwipe(true);
                }
                fragmentPagerAdapter = new VerticalViewPagerAdapter2(mActivity, videoList, videoIndex);
                fragmentPagerAdapter.setVerticalViewPagerAdapterCallBack(new VerticalViewPagerAdapter2.VerticalViewPagerAdapterCallBack()
                {
                    @Override
                    public void onUsernameOrHeaderClicked(int position)
                    {
                        // 通知上层 向右划
                        if (actionListener != null)
                            actionListener.onPageChange(1);
                    }

                    @Override
                    public void videoClicked(int position, ImageView btnPlay)
                    {
                        EmptyControlVideo emptyControlVideo = (EmptyControlVideo) fragmentPagerAdapter.getVideoMap().get(position).get("video");
                        if (emptyControlVideo != null)
                        {
                            IMediaPlayer iMediaPlayers = emptyControlVideo.getGSYVideoManager().getMediaPlayer();
                            if (iMediaPlayers != null)
                            {
                                if (iMediaPlayers.isPlaying())
                                {
                                    iMediaPlayers.pause();
                                    Animation btnPauseAnim = AnimationUtils.loadAnimation(mActivity, R.anim.video_pause_anim);
                                    btnPlay.setVisibility(View.VISIBLE);
                                    btnPlay.setAnimation(btnPauseAnim);
                                }
                                else
                                {
                                    try
                                    {
                                        iMediaPlayers.start();
                                        Animation btnPlayAnim = AnimationUtils.loadAnimation(mActivity, R.anim.video_play_anim);
                                        btnPlay.setVisibility(View.GONE);
                                        btnPlay.setAnimation(btnPlayAnim);
                                    } catch (IllegalStateException e)
                                    {
                                        e.printStackTrace();
                                        Video videoInfo = videoList.get(currentIndex);
                                        emptyControlVideo.setUp(videoInfo.getUrl(), true, null);
                                        emptyControlVideo.setStartAfterPrepared(true);
                                        emptyControlVideo.startPlayLogic();
                                    } catch (Resources.NotFoundException e)
                                    {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }

                    }

                    @Override
                    public void videoDoubleClicked(int position, LikeButton likeButton, TextView tvLikeNum)
                    {
//                        if (CommonUtils.checkUserStateFromFragment(VerticalVideoFragment.this, TAG))
//                        {
//                            model.startLikeVideo(videoList.get(position).getVideoId());
//                        }
                        if (likeButton.isLiked())
                            return;
                        likeButton.performClick();
//                        likeButtonLiked(position, likeButton, tvLikeNum);
                    }

                    @Override
                    public void likeButtonLiked(int position, LikeButton likeButton, TextView tvLikeNum)
                    {
                        tvLoveNum = tvLikeNum;

                        String videoId = videoList.get(position).getVideoId();

                        if (CommonUtils.checkUserStateFromFragment(VerticalVideoFragment.this, TAG))
                        {
                            model.checkVideoState(videoId).observe(VerticalVideoFragment.this, result -> {
                                if (result != null)
                                {
                                    if ("0".equals(result.get("result")))
                                    {
                                        isLiked = true;
                                        model.startLikeVideo(videoId);
                                    }
                                    else
                                    {
                                        ToastUtils.showShort("该视频已被删除");
                                    }
                                }
                            });
                        }
                        else
                            likeButton.setLiked(false);
                    }

                    @Override
                    public void likeButtonUnLiked(int position, LikeButton likeButton, TextView tvLikeNum)
                    {
                        tvLoveNum = tvLikeNum;
                        isLiked = false;
                    }

                    @Override
                    public void showCommentDialog(int position, TextView tvCommentNum)
                    {
                        tvCommentsNum = tvCommentNum;
                        String videoId = videoList.get(position).getVideoId();
                        if (CommonUtils.checkUserStateFromFragment(VerticalVideoFragment.this, TAG))
                        {
                            NetUtil.checkIsBindMobile(new NetUtilListener()
                            {
                                @Override
                                public void onSuccess()
                                {
                                    model.checkVideoState(videoId).observe(VerticalVideoFragment.this, result -> {
                                        if (result != null)
                                        {
                                            if ("0".equals(result.get("result")))
                                            {
                                                showCommentDialogView(position);
                                            }
                                            else
                                            {
                                                ToastUtils.showShort("该视频已被删除");
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFail()
                                {
                                    new CustomAlertDialog(mActivity)
                                            .builder()
                                            .setCancelable(true)
                                            .setMsg("当前未绑定手机号\n绑定后才可以操作哦\n")
                                            .setPositiveButton("去绑定", v ->
                                            {
                                                Intent intent = new Intent(mActivity, BindPhoneAct.class);
                                                mActivity.startActivity(intent);
                                            })
                                            .setNegativeButton("暂时不绑", v ->
                                            {
                                            }).show();
                                }
                            });
                        }
                    }

                    @Override
                    public void showCommentList(int position, TextView tvCommentNum)
                    {
                        tvCommentsNum = tvCommentNum;
                        CommentListDialog commentListDialog = new CommentListDialog();
                        Bundle bundle = new Bundle();
                        bundle.putString("videoId", videoList.get(position).getVideoId());
                        bundle.putString("videoSenderId", videoList.get(position).getUserId());
                        commentListDialog.setArguments(bundle);
                        commentListDialog.setCancelable(true);
                        commentListDialog.setCommentEventActionListener(new CommentListDialog.CommentEventActionListener()
                        {
                            @Override
                            public void onCommentSuccess()
                            {
                                int num;
                                try
                                {
                                    num = Integer.parseInt(tvCommentNum.getText().toString()) + 1;
                                    tvCommentNum.setText(String.valueOf(num));
                                } catch (Exception e)
                                {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onCommentDelete()
                            {
                                int num;
                                try
                                {
                                    num = Integer.parseInt(tvCommentNum.getText().toString()) - 1;
                                    tvCommentNum.setText(String.valueOf(num));
                                } catch (Exception e)
                                {
                                    e.printStackTrace();
                                }
                            }
                        });
                        commentListDialog.show(getFragmentManager(), CommentListDialog.class.getSimpleName());
                    }

                    @Override
                    public void showShareView(int position, TextView tvShareNums, BottomSheetLayout bottomSheetLayout)
                    {
                        tvShareNum = tvShareNums;
                        shareVideoId = videoList.get(position).getVideoId();
                        model.checkVideoState(shareVideoId).observe(VerticalVideoFragment.this, result -> {
                            if (result != null)
                            {
                                if ("0".equals(result.get("result")))
                                {
                                    showBottomSheet(position, bottomSheetLayout);
                                }
                                else
                                {
                                    ToastUtils.showShort("该视频已被删除");
                                }
                            }
                        });
                    }

                    @Override
                    public void onFocusAction(int position, TextView tvFocusView)
                    {
                        tvFocus = tvFocusView;
                        String videoId = videoList.get(position).getVideoId();
                        if (CommonUtils.checkUserStateFromFragment(VerticalVideoFragment.this, TAG))
                        {
                            model.checkVideoState(videoId).observe(VerticalVideoFragment.this, result -> {
                                if (result != null)
                                {
                                    if ("0".equals(result.get("result")))
                                    {
                                        model.focusPerson(videoList.get(position).getUserId());
                                    }
                                    else
                                    {
                                        ToastUtils.showShort("该视频已被删除");
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void deleteVideo(int position)
                    {
                        new CustomAlertDialog(mActivity)
                                .builder()
                                .setMsg("是否删除该视频")
                                .setPositiveButton("是", new View.OnClickListener()
                                {
                                    @Override
                                    public void onClick(View v)
                                    {
                                        model.deleteVideo(position, videoList.get(position).getVideoId());
                                    }
                                })
                                .setNegativeButton("否", new View.OnClickListener()
                                {
                                    @Override
                                    public void onClick(View v)
                                    {

                                    }
                                })
                                .show();
                    }
                });
                verticalViewPager.setAdapter(fragmentPagerAdapter);
                verticalViewPager.setCurrentItem(videoIndex, false);
                if (actionListener != null)
                    actionListener.loadUserInfo(videoList.get(videoIndex).getUserId());

                mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
                {
                    @Override
                    public void onRefresh()
                    {

                        String videoId = videoList.get(0).getVideoId();
                        getDataDirectory = "1";
                        model.startGetMoreVideoDetail(videoId, openSource, toUserId, getDataDirectory);
//
//
//                        ToastUtils.showLong("我被刷新了，杨启航请在这里调用刷新数据接口，调用完成后，将刷新圈去掉");
//                        new Handler().postDelayed(new Runnable()
//                        {
//                            @Override
//                            public void run()
//                            {
//                                mySwipeRefreshLayout.setRefreshing(false);
//                            }
//                        },1000);
                    }
                });

            }
        });

        model.getMoreVideoDetail().observe(this, moreVideoDetailEntity -> {
            if (moreVideoDetailEntity != null)
            {
                List<Video> returnVideos = moreVideoDetailEntity.getVideos();
                if ("1".equals(getDataDirectory))
                {
                    if (returnVideos.size() == 1)
                        mySwipeRefreshLayout.setNeedSwipe(true);
                    else
                        mySwipeRefreshLayout.setNeedSwipe(false);
                    returnVideos.removeAll(videoList);
                    currentIndex = returnVideos.size();
                    videoList.addAll(0, returnVideos);
                    mySwipeRefreshLayout.setRefreshing(false);
//                    videoInfoList.addAll(0, moreVideoDetailEntity.getVideos());
                }
                else if ("2".equals(getDataDirectory))
                {
                    returnVideos.removeAll(videoList);
                    videoList.addAll(returnVideos);
                }

//                fragmentPagerAdapter = new VerticalViewPagerAdapter(mActivity, currentIndex, getFragmentManager(), videoInfoList, verticalViewPager, openSource);
//                verticalViewPager.setAdapter(fragmentPagerAdapter);

                fragmentPagerAdapter.notifyDataSetChanged();
                verticalViewPager.setCurrentItem(currentIndex);
//                EmptyControlVideo emptyControlVideo = fragmentPagerAdapter.getVideoMap().get(currentIndex);
//                if (emptyControlVideo != null)
//                    emptyControlVideo.startPlayLogic();
            }
        });

        model.getLikeData().observe(this, result -> {
            if (!TextUtils.isEmpty(result))
            {
                try
                {
                    int likeNum = 0;
                    if (isLiked)
                    {
                        likeNum = Integer.parseInt(tvLoveNum.getText().toString()) + 1;
//                        ivLove.setLiked(false);
                    }
                    else
                    {
                        //暂时保留  产品让 只加 不减
//                        likeNum = Integer.parseInt(tvLoveNum.getText().toString()) - 1;
                        likeNum = Integer.parseInt(tvLoveNum.getText().toString());
//                        ivLove.setLiked(true);
                    }
                    tvLoveNum.setText(String.valueOf(likeNum));
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        model.getCommentDetail().observe(this, result -> {
            if (!TextUtils.isEmpty(result))
            {
                if (inputMsgDialog != null)
                {
                    inputMsgDialog.dismiss();
                    ToastUtils.showShort("评论成功");
                    int num;
                    try
                    {
                        num = Integer.parseInt(tvCommentsNum.getText().toString()) + 1;
                        tvCommentsNum.setText(String.valueOf(num));
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        });
        model.getShareVideo().observe(this, result -> {
            //分享视频成功
            Logger.t(TAG).d("分享视频成功");
        });
        model.getFocusResult().observe(this, result -> {
            if (!TextUtils.isEmpty(result))
            {
                tvFocus.setVisibility(View.GONE);
                if (actionListener != null)
                {
                    actionListener.onFocus();
                }
            }
        });

        model.getDeleteVideo().observe(this, result -> {
            if (result != null)
            {
                Logger.t(TAG).d("删除result：" + result);
                if (videoList.size() - 1 <= 0)
                {
                    mActivity.finish();
                }
                if ("1".equals(openSource))
                    mActivity.finish();
                if (result < videoList.size())
                {
                    verticalViewPager.setCurrentItem(result + 1, false);
                }
                else
                    verticalViewPager.setCurrentItem(result - 1, false);

//                verticalViewPager.setCurrentItem(result + 1, false);
//                ((EmptyControlVideo) fragmentPagerAdapter.getVideoMap().get(result).get("video")).release();
//                fragmentPagerAdapter.getVideoMap().remove(result);
                int index = result;
                videoList.remove(index);
                fragmentPagerAdapter.notifyDataSetChanged();
                verticalViewPager.setCurrentItem(index, false);
            }

        });


        model.startGetVideoDetail(videoId, openSource, toUserId, getDataDirectory);

    }

    public void refreshFocusState()
    {
        fragmentPagerAdapter.hideFocusView();
    }

    public void onVideoResume()
    {
        try
        {
            EmptyControlVideo emptyControlVideo = (EmptyControlVideo) fragmentPagerAdapter.getVideoMap().get(currentIndex).get("video");
            ImageView imageView = (ImageView) fragmentPagerAdapter.getVideoMap().get(currentIndex).get("image");

            if (emptyControlVideo != null)
            {
                IMediaPlayer iMediaPlayers = emptyControlVideo.getGSYVideoManager().getMediaPlayer();
                if (iMediaPlayers != null)
                {
//                    if (fragmentPagerAdapter.isPrepared())
                    {
                        iMediaPlayers.start();
                        imageView.setVisibility(View.GONE);
                    }
                }
            }
        } catch (IllegalStateException e)
        {
            e.printStackTrace();
            EmptyControlVideo emptyControlVideo = (EmptyControlVideo) fragmentPagerAdapter.getVideoMap().get(currentIndex).get("video");
            Video videoInfo = videoList.get(currentIndex);
            emptyControlVideo.setUp(videoInfo.getUrl(), true, null);
            emptyControlVideo.setStartAfterPrepared(true);
            emptyControlVideo.startPlayLogic();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void onVideoPause()
    {
        try
        {
            EmptyControlVideo emptyControlVideo = (EmptyControlVideo) fragmentPagerAdapter.getVideoMap().get(currentIndex).get("video");
            if (emptyControlVideo != null)
            {
                IMediaPlayer iMediaPlayers = emptyControlVideo.getGSYVideoManager().getMediaPlayer();
                if (iMediaPlayers != null)
                {
                    //                if (fragmentPagerAdapter.isPrepared())
                    if (iMediaPlayers.isPlaying())
                    {
                        iMediaPlayers.pause();
                    }
                }
            }
        } catch (IllegalStateException e)
        {
            e.printStackTrace();
        }
    }


    private void showCommentDialogView(int position)
    {
        inputMsgDialog = new InputTextMsgDialog();
        inputMsgDialog.setCancelable(true);
        inputMsgDialog.setOnInputFinishedListener(inputContent ->
        {
            if (TextUtils.isEmpty(inputContent))
            {
                ToastUtils.showShort("请输入评论内容");
                return;
            }
            model.startCommentVideo(videoList.get(position).getVideoId(), inputContent);
        });
        inputMsgDialog.show(mActivity.getFragmentManager(), InputTextMsgDialog.class.getSimpleName());
    }

    private void showBottomSheet(int position, BottomSheetLayout bottomSheetLayout)
    {

        if (bottomSheet == null)
        {
            bottomSheet = createBottomSheetView(position, bottomSheetLayout);
        }
        bottomSheetLayout.addOnSheetDismissedListener(new OnSheetDismissedListener()
        {
            @Override
            public void onDismissed(BottomSheetLayout bottomSheetLayout)
            {
                verticalViewPager.setStopScroll(false);
            }
        });
        if (bottomSheetLayout.isSheetShowing())
        {
            bottomSheetLayout.dismissSheet();
            verticalViewPager.setStopScroll(false);
        }
        else
        {
            if (sharesName.size() != 0)
            {
                bottomSheetLayout.showWithSheetView(bottomSheet);
                verticalViewPager.setStopScroll(true);
            }
        }
    }

    private View createBottomSheetView(int position, BottomSheetLayout bottomSheetLayout)
    {
        String videoUrl = videoList.get(position).getUrl();
        String userId = videoList.get(position).getUserId();


        ShareToFaceBean shareBean = new ShareToFaceBean();
        shareBean.setShareType(Platform.SHARE_WEBPAGE);
        String title = "小录段子";
        if (!TextUtils.isEmpty(videoList.get(position).getContent()))
        {
            title = videoList.get(position).getContent();
        }
        shareBean.setShareTitle(title);
//        shareBean.setShareTitleUrl(NetHelper.LIVE_SHARE_ADDRESS + SharePreUtils.getTlsName(mAct).substring(1));
        shareBean.setShareWeChatMomentsTitle(title);
        String shareUrl = String.format(Api.SHARE_URL + "?userid=%s&videoid=%s", SharePreUtils.getUId(), videoList.get(position).getVideoId());
        shareBean.setShareUrl(shareUrl);
        shareBean.setShareImgUrl(videoList.get(position).getThumbnails());
        shareBean.setShareContent("小录段子，流行风向标");
//        shareBean.setShareWeChatMomentsContent("越帅越优惠 越靓越实惠,看脸吃饭 【" + roomName + "】");
//        shareBean.setShareSinaContent("我的直播间是“" + roomName + "”,颜值高不高，直播才知道，赶快来给我送礼物吧！" + NetHelper.LIVE_SHARE_ADDRESS + SharePreUtils.getTlsName(mAct).substring(1));
//        shareBean.setShareAppImageUrl(NetHelper.LIVE_SHARE_PIC);
//        shareBean.setShareSite("看脸吃饭");
//        shareBean.setOpenSouse("liveShare");
//        shareBean.setRoomId(SharePreUtils.getId(mAct));
//        shareBean.setRoomName(SharePreUtils.getNicName(mAct));
//        shareBean.setShareSiteUrl(NetHelper.SERVER_SITE);
        shareBean.setShareListener(shareListener);


//        sharesName.add("新浪微博");
//        sharesName.add("看脸好友");
        sharesName.add("微信好友");
        sharesName.add("朋友圈");
//        sharesName.add("QQ好友");
//        sharesName.add("QQ空间");
        sharesName.add("举报");
        sharesName.add("存至相册");
        sharesName.add("复制链接");

        View view = LayoutInflater.from(mActivity).inflate(R.layout.pop_shares_list, (ViewGroup) mActivity.getWindow().getDecorView(), false);
        RecyclerView rvShares = view.findViewById(R.id.rvShares);
        sharesAdapter = new SharesAdapter(mActivity, sharesName);
        rvShares.setAdapter(sharesAdapter);
        sharesAdapter.setOnItemClickListener((vItem, shareKey) -> {
            switch (shareKey)
            {
                case "微信好友":
                    CommonUtils.shareWithApp(mActivity, "微信好友", shareBean);
                    break;
                case "朋友圈":
                    CommonUtils.shareWithApp(mActivity, "微信朋友圈", shareBean);
                    break;
                case "举报":
                    if (CommonUtils.checkUserStateFromFragment(VerticalVideoFragment.this, TAG))
                    {
                        Intent intent = new Intent(getActivity(), ReportFoulsUserAct.class);
                        intent.putExtra("uId", userId);
                        startActivity(intent);
                    }
                    break;
                case "存至相册":
                    VideoUtils.saveVideoToMediaLib(mActivity,videoUrl);
                    if (bottomSheetLayout.isSheetShowing())
                    {
                        bottomSheetLayout.dismissSheet();
                        verticalViewPager.setStopScroll(false);
                    }

                    break;
                case "复制链接":
                    ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                    // 将文本内容放到系统剪贴板里。
                    cm.setText(videoUrl);
                    ToastUtils.showShort("复制成功");
                    break;
                default:
                    CommonUtils.shareWithApp(mActivity, shareKey, shareBean);
            }
        });


        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4)
        {
            @Override
            protected int getExtraLayoutSpace(RecyclerView.State state)
            {
                return 300;
            }
        };
        //设置布局管理器 , 将布局设置成纵向
//        LinearLayoutManager linerLayoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
        rvShares.setLayoutManager(gridLayoutManager);

        //设置分隔线
        //mRecycleView.addItemDecoration(new DividerItemDecoration(this , DividerItemDecoration.VERTICAL_LIST));

        //设置增加或删除条目动画
        rvShares.setItemAnimator(new DefaultItemAnimator());

        return view;
    }

    PlatformActionListener shareListener = new PlatformActionListener()
    {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap)
        {
            mActivity.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    model.shareVideo(shareVideoId);
                    ToastUtils.showShort("分享成功");
                    int shareNum;
                    try
                    {
                        shareNum = Integer.parseInt(tvShareNum.getText().toString());
                        tvShareNum.setText(++shareNum + "");
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable)
        {
            mActivity.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    ToastUtils.showShort("分享失败");
                }
            });

        }

        @Override
        public void onCancel(Platform platform, int i)
        {
            mActivity.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    model.shareVideo(shareVideoId);
                    ToastUtils.showShort("分享取消");
                    int shareNum;
                    try
                    {
                        shareNum = Integer.parseInt(tvShareNum.getText().toString());
                        tvShareNum.setText(++shareNum + "");
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
    };

    @Override
    public void onPause()
    {
        super.onPause();
        Logger.t(TAG).d("onPause()");
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        if (fragmentPagerAdapter != null)
        {
            for (Map<String, Object> stringObjectMap : fragmentPagerAdapter.getVideoMap().values())
            {
                ((EmptyControlVideo) stringObjectMap.get("video")).release();
            }
            fragmentPagerAdapter.getVideoMap().clear();
        }
        unbinder.unbind();
    }


    @Override
    protected String getPageName()
    {
        return TAG;
    }

    private IOnVerticalPagerActionListener actionListener;


    public interface IOnVerticalPagerActionListener
    {
        void onPageChange(int position);

        void onFocus();

        void loadUserInfo(String otherUid);

    }

    public void setOnVerticalPagerActionListener(IOnVerticalPagerActionListener listener)
    {
        actionListener = listener;
    }

}